package com.rock.upms.svc.upms.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rock.upms.api.model.service.IUpmsPermissionService;
import com.rock.upms.api.upms.service.IMenuService;
import com.rock.upms.api.upms.service.IUpmsService;
import com.rock.upms.model.UpmsPermission;
import com.rock.upms.model.UpmsPermissionExample;
import com.rock.upms.model.dto.ElementMenu;
import com.rock.upms.model.dto.RockMenu;

/**
 * 菜单服务类
 * 
 * @Description:TODO
 * @author: hotdog
 * @date: 2018年8月30日 下午4:34:49
 * @Copyright: v1.0
 *
 */
@Service
public class MenuService implements IMenuService {

	@Autowired
	private IUpmsPermissionService upmsPermissionService;
	@Autowired
	private IUpmsService upmsService;

	@Override
	public List<RockMenu> getUserPrimaryMenu(int upmsUser_Id) {
		// 参数0表示主菜单即系统菜单
		return getNextFloorMenu(upmsUser_Id, 0);
	}

	@Override
	public List<RockMenu> getUserSubmenu(int upmsUser_id, int upmsPermission_id) {
		List<Integer> permissionIds = getPermissionIds(upmsUser_id);
		// 递归获取菜单
		return getChildMenuAll("1", upmsPermission_id, 0, permissionIds);
	}

	@Override
	public List<RockMenu> getSubmenu(int upmsPermission_id) {
		return getChildMenuAll("1", upmsPermission_id, 0);
	}

	@Override
	public List<RockMenu> getNextFloorMenu(int upmsUser_Id, int perentId) {
		List<Integer> permissionIds = getPermissionIds(upmsUser_Id);
		List<RockMenu> temp = new ArrayList<RockMenu>();
		// 菜单查询实例2表示菜单 9表示显示
		UpmsPermissionExample childMenuExample = new UpmsPermissionExample();
		childMenuExample.or().andTypeEqualTo("2").andVisualTypeNotEqualTo("9").andPidEqualTo(perentId).andIdIn(permissionIds);
		childMenuExample.setOrderByClause(" seqno asc ");
		List<UpmsPermission> childMenuList = upmsPermissionService.selectByExample(childMenuExample);
		if (childMenuList != null && childMenuList.size() > 0) {
			for (int i = 0; i < childMenuList.size(); i++) {
				// 子菜单
				RockMenu menu = new RockMenu();
				menu.setId(childMenuList.get(i).getId());
				// 菜单编号1-1,1-2,1-2-1
				menu.setMenuNo(1 + "-" + String.valueOf(i + 1));
				// 菜单名称
				menu.setMenuName(childMenuList.get(i).getName());
				// 菜单等级，1,2,3,4,5...
				menu.setRank(1);
				menu.setUrl(childMenuList.get(i).getUri());
				menu.setRemarks(childMenuList.get(i).getRemarks());
				menu.setSeqno(childMenuList.get(i).getSeqno());
				menu.setIcon(childMenuList.get(i).getIcon());
				temp.add(menu);
			}
		}
		return temp;
	}

	@Override
	public List<RockMenu> getPramaryMenu() {
		List<RockMenu> temp = new ArrayList<RockMenu>();
		// 菜单查询实例2表示菜单 9表示显示 0表示系统菜单
		UpmsPermissionExample childMenuExample = new UpmsPermissionExample();
		childMenuExample.or().andTypeEqualTo("2").andVisualTypeNotEqualTo("9").andPidEqualTo(0);
		childMenuExample.setOrderByClause(" seqno asc ");
		List<UpmsPermission> childMenuList = upmsPermissionService.selectByExample(childMenuExample);
		if (childMenuList != null && childMenuList.size() > 0) {
			for (int i = 0; i < childMenuList.size(); i++) {
				// 子菜单
				RockMenu menu = new RockMenu();
				menu.setId(childMenuList.get(i).getId());
				// 菜单编号1-1,1-2,1-2-1
				menu.setMenuNo(1 + "-" + String.valueOf(i + 1));
				// 菜单名称
				menu.setMenuName(childMenuList.get(i).getName());
				// 菜单等级，1,2,3,4,5...
				menu.setRank(1);
				menu.setUrl(childMenuList.get(i).getUri());
				menu.setRemarks(childMenuList.get(i).getRemarks());
				menu.setSeqno(childMenuList.get(i).getSeqno());
				menu.setIcon(childMenuList.get(i).getIcon());
				temp.add(menu);
			}
		}
		return temp;
	}

	/**
	 * 获取用户的权限ID集合
	 * 
	 * @param upmsUser_Id
	 * @return
	 */
	private List<Integer> getPermissionIds(int upmsUser_Id) {
		List<UpmsPermission> userPermissionList = upmsService.getPermissionByUserId(upmsUser_Id);
		// 所有权限ID集合
		List<Integer> permissionIds = new ArrayList<>();
		// 所有系统菜单ID集合
		if (userPermissionList != null) {
			int size = userPermissionList.size();
			for (int i = 0; i < size; i++) {
				permissionIds.add(userPermissionList.get(i).getId());
			}
		}
		return permissionIds;
	}

	/**
	 * 递归获取所有子菜单，不过滤权限
	 * 
	 * @param no
	 * @param perentId
	 * @param rank
	 * @return
	 */
	private List<RockMenu> getChildMenuAll(String no, int perentId, int rank) {
		// 每递归一次，级别加1
		int ranktemp = rank + 1;
		List<RockMenu> temp = new ArrayList<RockMenu>();
		// 菜单查询实例
		UpmsPermissionExample childMenuExample = new UpmsPermissionExample();
		childMenuExample.or().andTypeEqualTo("2").andVisualTypeNotEqualTo("9").andPidEqualTo(perentId);
		childMenuExample.setOrderByClause(" seqno asc ");
		List<UpmsPermission> childMenuList = upmsPermissionService.selectByExample(childMenuExample);
		if (childMenuList != null && childMenuList.size() > 0) {
			for (int i = 0; i < childMenuList.size(); i++) {
				// 子菜单
				RockMenu menu = new RockMenu();
				menu.setId(childMenuList.get(i).getId());
				// 菜单编号1-1,1-2,1-2-1
				menu.setMenuNo(no + "-" + String.valueOf(i + 1));
				// 菜单名称
				menu.setMenuName(childMenuList.get(i).getName());
				// 菜单等级，1,2,3,4,5...
				menu.setRank(ranktemp);
				menu.setUrl(childMenuList.get(i).getUri());
				menu.setRemarks(childMenuList.get(i).getRemarks());
				menu.setSeqno(childMenuList.get(i).getSeqno());
				menu.setIcon(childMenuList.get(i).getIcon());
				temp.add(menu);
				temp.addAll(getChildMenuAll(no + "-" + String.valueOf(i + 1), childMenuList.get(i).getId(), ranktemp));
			}
		}
		return temp;
	}

	/**
	 * 功能：获取子菜单<br/>
	 * 说明：利用递归的形式获取子菜单，过滤权限
	 * 
	 * @param no
	 *            菜单编号 1-1,1-2,1-2-1形式，获取系统ID对应的菜单时 直接传1
	 * @param perentId
	 *            父节点ID
	 * @param rank
	 *            菜单级别 获取系统ID对应的菜单时传入1
	 * @param permissionIds
	 *            登录人权限ID集合
	 * @author zhousq
	 * @version 创建时间：2017-11-11 14:04:37
	 * @return 菜单列表List<RockMenu>
	 */
	private List<RockMenu> getChildMenuAll(String no, int perentId, int rank, List<Integer> permissionIds) {
		// 每递归一次，级别加1
		int ranktemp = rank + 1;
		List<RockMenu> temp = new ArrayList<RockMenu>();
		// 菜单查询实例
		UpmsPermissionExample childMenuExample = new UpmsPermissionExample();
		childMenuExample.or().andTypeEqualTo("2").andVisualTypeNotEqualTo("9").andPidEqualTo(perentId).andIdIn(permissionIds);
		childMenuExample.setOrderByClause(" seqno asc ");
		List<UpmsPermission> childMenuList = upmsPermissionService.selectByExample(childMenuExample);
		if (childMenuList != null && childMenuList.size() > 0) {
			for (int i = 0; i < childMenuList.size(); i++) {
				// 子菜单
				RockMenu menu = new RockMenu();
				menu.setId(childMenuList.get(i).getId());
				// 菜单编号1-1,1-2,1-2-1
				menu.setMenuNo(no + "-" + String.valueOf(i + 1));
				// 菜单名称
				menu.setMenuName(childMenuList.get(i).getName());
				// 菜单等级，1,2,3,4,5...
				menu.setRank(ranktemp);
				menu.setUrl(childMenuList.get(i).getUri());
				menu.setRemarks(childMenuList.get(i).getRemarks());
				menu.setSeqno(childMenuList.get(i).getSeqno());
				menu.setIcon(childMenuList.get(i).getIcon());
				temp.add(menu);
				temp.addAll(getChildMenuAll(no + "-" + String.valueOf(i + 1), childMenuList.get(i).getId(), ranktemp, permissionIds));
			}
		}
		return temp;
	}

	// 递归调用方式获取
	@Override
	public List<ElementMenu> getElementMenu(int upmsUser_Id, int perentId) {
		List<Integer> permissionIds = getPermissionIds(upmsUser_Id);
		List<ElementMenu> resultList = new ArrayList<ElementMenu>();
		// 菜单查询实例2表示菜单 9表示显示
		UpmsPermissionExample childMenuExample = new UpmsPermissionExample();
		childMenuExample.or().andTypeEqualTo("2").andVisualTypeNotEqualTo("9").andPidEqualTo(perentId).andIdIn(permissionIds);
		childMenuExample.setOrderByClause(" seqno asc ");
		List<UpmsPermission> childMenuList = upmsPermissionService.selectByExample(childMenuExample);
		if (childMenuList != null && childMenuList.size() > 0) {
			for (int i = 0; i < childMenuList.size(); i++) {
				// 子菜单
				ElementMenu elementMuenu = new ElementMenu();
				elementMuenu.setIcon(childMenuList.get(i).getIcon());
				elementMuenu.setName(childMenuList.get(i).getName());
				elementMuenu.setPath(childMenuList.get(i).getUri());
				//递归调用
				List<ElementMenu> childElementMenuList = getElementMenu(upmsUser_Id, childMenuList.get(i).getId());
				if (childElementMenuList != null && childElementMenuList.size() > 0) {
					elementMuenu.setChildren(childElementMenuList);
				}
				resultList.add(elementMuenu);
			}
		}
		return resultList;
	}
}
